﻿using System;
using System.IO;
using System.Web.UI;
using SouthernLand.Common;
using SouthernLand.Common.Untilities;

namespace SouthernLand.WebMaster.Advertise
{
    public partial class AdvertiseAdd : Page
    {
        private int AdvID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            AdvID = ConvertUtility.ToInt32(Request.QueryString["AdvID"]);
            if (AdvID > 0)
            {
                btSave.Text = " Update ";
                PhotoFile.Visible = false;
                if (!IsPostBack)
                {
                    Load_GetAdvertiseByAdvID(AdvID);
                }
            }
        }

        protected string FilePhoto_Upload()
        {
            string strFileName = "";
            if (PhotoFile.HasFile)
            {
                string strExtension = Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" ||
                    strExtension == ".PNG" || strExtension == ".SWF")
                {
                    strFileName = Common.Common.GetFileName() + Path.GetExtension(PhotoFile.FileName);
                    string strFilePathLarge = Request.PhysicalApplicationPath + "UserUpload/Logo/" + strFileName;
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, -1);
                }
                else
                {
                    PanelMsg.Visible = true;
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    strFileName = "";
                }
            }

            return strFileName;
        }

        protected void Load_GetAdvertiseByAdvID(int AdvID)
        {
            Common.BOL.Advertise.Advertise obj = Common.BOL.Advertise.Advertise.GetAdvertiseByID(AdvID);
            imgLogo.Visible = true;
            imgLogo.ImageUrl = "~/UserUpload/Logo/" + obj.FileName;
            txtUrl.Text = obj.AdvURL;
            txtOrder.Text = obj.DisplayOrder.ToString();
            dlPosition.SelectedValue = obj.AdvPosition.ToString();
            hImage.Value = obj.FileName;
            txtWidth.Text = obj.AdvWidth.ToString();
            txtHeight.Text = obj.AdvHeight.ToString();
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            // cap nhat du lieu vao database
            var obj = new Common.BOL.Advertise.Advertise();
            obj.AdvID = AdvID;
            obj.AdvURL = txtUrl.Text;
            obj.RegionDate = DateTime.Now;
            obj.ExpireDate = DateTime.MaxValue;

            if (txtRegionDate.Text != "")
                obj.RegionDate = Convert.ToDateTime(txtRegionDate.Text);
            if (txtExpireDate.Text != "")
                obj.ExpireDate = Convert.ToDateTime(txtExpireDate.Text);

            obj.AdvPosition = ConvertUtility.ToInt32(dlPosition.SelectedItem.Value);
            obj.DisplayOrder = ConvertUtility.ToInt32(txtOrder.Text);
            obj.AddedDate = DateTime.Now;
            obj.IsActive = true;
            obj.AdvWidth = ConvertUtility.ToInt32(txtWidth.Text);
            obj.AdvHeight = ConvertUtility.ToInt32(txtHeight.Text);

            if (Globals.GetIntFromQueryString("AdvID") > 0)
            {
                obj.FileName = hImage.Value;
                Common.BOL.Advertise.Advertise.Update(obj);
            }
            else
            {
                obj.FileName = FilePhoto_Upload();
                Common.BOL.Advertise.Advertise.Insert(obj);
            }
            Response.Redirect(Globals.ApplicationPath + "WebMaster/Advertise");
        }
    }
}